using System;
using System.Windows.Forms;
using FBroSharp.Lib;        // ⚠️ CRITICAL! IFBroSharpBrowser, IFBroSharpFrame, IFBroSharpUseExtraData
using FBroSharp.Event;      // FBroSharpBrowserEvent
using FBroSharp.DataType;   // ⚠️ REQUIRED! Additional data types
using FBroSharp.Value;      // ⚠️ CRITICAL! IFBroSharpDictionaryValue, FBroSharpWindowsInfo, FBroSharpBrowserSetting, FBroSharpPopupfeatures
using FBroSharp.Const;      // FBroSharpTransitionType, FBroSharpWindowOpenDisposition

namespace _4_表单批量填写器
{
    /// <summary>
    /// 浏览器事件处理器 - 创建浏览器时必需
    /// </summary>
    public class BrowserEvent : FBroSharpBrowserEvent
    {
        /// <summary>
        /// ⚠️ REQUIRED: 浏览器创建完成 (必须有两个参数!)
        /// </summary>
        public override void OnAfterCreated(IFBroSharpBrowser browser, IFBroSharpDictionaryValue extrainfo)
        {
            Console.WriteLine($"浏览器创建完成，ID：{browser.GetIdentifier()}");
        }

        /// <summary>
        /// ⚠️ CRITICAL FOR SINGLE-WINDOW APPS: 弹窗控制 - 防止打开新窗口
        /// 这个方法对于单窗口应用程序是必需的!
        /// 没有这个方法，target="_blank" 的链接会打开新浏览器窗口。
        /// </summary>
        public override bool OnBeforePopup(
            IFBroSharpBrowser browser, 
            IFBroSharpFrame frame, 
            string target_url, 
            string target_frame_name,
            FBroSharpWindowOpenDisposition target_disposition, 
            bool user_gesture, 
            FBroSharpPopupfeatures popupFeatures, 
            ref FBroSharpWindowsInfo windowInfo,    // Note: ref parameter!
            ref FBroSharpBrowserSetting settings,   // Note: ref parameter!
            ref bool no_javascript_access, 
            IFBroSharpUseExtraData user_settings)
        {
            // 在当前窗口中打开，而不是新窗口
            frame.LoadURL(target_url);
            return true;  // true = 阻止弹窗, false = 允许弹窗
        }

        /// <summary>
        /// ⚠️ REQUIRED: 地址变更
        /// </summary>
        public override void OnAddressChange(IFBroSharpBrowser browser, IFBroSharpFrame frame, string url)
        {
            if (frame.IsMain())
            {
                Console.WriteLine($"URL 变更: {url}");
            }
        }

        /// <summary>
        /// ⚠️ REQUIRED: 标题变更
        /// </summary>
        public override void OnTitleChange(IFBroSharpBrowser browser, string title)
        {
            Console.WriteLine($"标题变更: {title}");
        }

        /// <summary>
        /// ⚠️ REQUIRED: 加载状态变更
        /// </summary>
        public override void OnLoadingStateChange(IFBroSharpBrowser browser, bool isLoading, bool canGoBack, bool canGoForward)
        {
            Console.WriteLine($"加载状态: {(isLoading ? "加载中" : "加载完成")}");
        }

        /// <summary>
        /// ⚠️ REQUIRED: 页面加载完成
        /// </summary>
        public override void OnLoadEnd(IFBroSharpBrowser browser, IFBroSharpFrame frame, int httpStatusCode)
        {
            if (frame.IsMain())
            {
                Console.WriteLine($"加载完成 (HTTP {httpStatusCode})");
            }
        }

        /// <summary>
        /// ⚠️ REQUIRED: 页面加载错误 (errorCode 是 int 类型!)
        /// </summary>
        public override void OnLoadError(IFBroSharpBrowser browser, IFBroSharpFrame frame, 
            int errorCode, string errorText, string failedUrl)
        {
            if (frame.IsMain())
            {
                Console.WriteLine($"加载失败: {errorText}");
            }
        }

        /// <summary>
        /// ⚠️ REQUIRED: 浏览器关闭前
        /// </summary>
        public override void OnBeforeClose(IFBroSharpBrowser browser)
        {
            // 清理资源
        }

        /// <summary>
        /// ⚠️ REQUIRED: 浏览器是否可以关闭
        /// </summary>
        public override bool DoClose(IFBroSharpBrowser browser)
        {
            return false; // false = 允许关闭
        }
    }
}